package AAA;

import java.util.Arrays;

public class Stack<T> {
    public int[] elem;
    public int useSize;
    public static final int DEFAULT_CAPACITY = 10;

    public Stack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }



    public void push(int val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[useSize++] = val;
    }

    private boolean isFull() {
        ;
        return useSize == elem.length;
    }

    public int pop() {
        if (isEmpty()) {
            throw new EmptyException("栈已空，无法出栈！");
        }
        int olderVal = elem[useSize - 1];
        useSize--;
        return olderVal;
    }

    private boolean isEmpty() {
        return useSize == 0;
    }

    public int peek() {
        if (isEmpty()) {
            throw new EmptyException("栈已空，无法出栈！");
        }
        return elem[useSize - 1];
    }

    public void display() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(elem[i] + " ");
        }
    }

    //练习
    //Character.isDigit方法：确定或判断指定字符是否是一个数字。
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if (! isOperation(str)) {
                int val = Integer.valueOf(str);
                stack.push(val);
            } else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (str) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOperation(String str) {
        if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {
            return true;
        }
        return false;
    }
    //2.括号匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if  (stack.isEmpty()) {
                    return false;
                } else {
                    char ch2 = (char) stack.peek();
                    if (ch == ')' && ch2 == '(' || ch == '}' && ch2 == '{' || ch == ']' && ch2 == '[') {
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }
        if (!stack.isEmpty()) {
            return false;
        }
        return true;
    }
//3.栈的压入、弹出序列
    public boolean IsPopOrder(int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.isEmpty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return j >= popV.length;
        //return stack.empty();
    }

}